package cn.chenshiwu121.service.impl;

import cn.chenshiwu121.mapper.ItemMapper;
import cn.chenshiwu121.pojo.EasyUIDataGridResult;
import cn.chenshiwu121.pojo.Item;
import cn.chenshiwu121.pojo.ItemExample;
import cn.chenshiwu121.service.ItemService;
import cn.chenshiwu121.utils.IDUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemMapper mapper;

    @Override
    public EasyUIDataGridResult<Item> getItemList(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        ItemExample example = new ItemExample();
        List<Item> list = mapper.selectByExample(example);
        PageInfo<Item> info = new PageInfo<>(list);
        EasyUIDataGridResult<Item> result = new EasyUIDataGridResult<>();
        result.setTotal((int)info.getTotal());
        result.setRows(list);
        return result;
    }

    @Override
    public void save(Item item) throws Exception {
        // 生成唯一id
        item.setId(IDUtils.genItemId());
        item.setStatus((byte) 1);
        item.setCreated(new Date());
        item.setUpdated(item.getCreated());
        int rows = mapper.insert(item);
        if (rows < 1) throw new Exception("ERROR:save item failed");
    }

    @Override
    public void updateItemById(Item item) throws Exception {
        item.setUpdated(new Date());
        int rows = mapper.updateByPrimaryKeySelective(item);
        if (rows < 1) throw new Exception("ERROR:update item failed");
    }

    @Override
    public void removeItemById(String ids) throws Exception {
        String[] idArr = ids.split(",");
        List<Long> idList = new ArrayList<>();
        for (String id: idArr) {
            idList.add(Long.valueOf(id));
        }
        ItemExample example = new ItemExample();
        ItemExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(idList);
        int rows = mapper.deleteByExample(example);
        if (rows < idArr.length) throw new Exception("ERROR:delete items failed");
    }

    @Override
    public void instockItemById(String ids) throws Exception {
        String[] idArr = ids.split(",");
        List<Long> idList = new ArrayList<>();
        for (String id: idArr) {
            idList.add(Long.valueOf(id));
        }
        ItemExample example = new ItemExample();
        ItemExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(idList);
        List<Item> items = mapper.selectByExample(example);
        for (Item item: items) {
            item.setStatus((byte) 2); // 下架
            mapper.updateByPrimaryKey(item);
        }
    }

    @Override
    public void reshelfItemById(String ids) throws Exception {
        String[] idArr = ids.split(",");
        List<Long> idList = new ArrayList<>();
        for (String id: idArr) {
            idList.add(Long.valueOf(id));
        }
        ItemExample example = new ItemExample();
        ItemExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(idList);
        List<Item> items = mapper.selectByExample(example);
        for (Item item: items) {
            item.setStatus((byte) 1); // 上架
            mapper.updateByPrimaryKey(item);
        }
    }
}
